package com.chidopi.cdf.struts1actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chidopi.cdf.domain.BaseVO;
import com.chidopi.cdf.service.IBaseService;


public abstract class StandardOperatorAction<T extends BaseVO> extends CdfDispatchAction implements
		IStandardOperator {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	protected IBaseService<T> baseService;

	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DynaBean dynaForm = (DynaBean) form;
		dynaForm.set("vo", null);
		dynaForm.set("op", null);
		return mapping.findForward("success");
	}

	public ActionForward back(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("success");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		logger.debug("id=" + id);
		T t = this.baseService.findById(new Integer(id));
		if (null == t) {
			logger.warn("Account[id={}] is deleted!!", id);
			return mapping.findForward("err");
		}
		DynaBean dynaForm = (DynaBean) form;
		dynaForm.set("op", "delete");
		dynaForm.set("vo", t);
		return mapping.findForward("success");
	}

	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		logger.debug("id=" + id);
		T t = this.baseService.findById(id);
		if (null == t) t = this.newValueObject();
		//t.setId(this.baseService.getNextSequence());
		DynaBean dynaForm = (DynaBean) form;
		dynaForm.set("op", "insert");
		dynaForm.set("vo", t);
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("id");
		logger.debug("id=" + id);
		T t = this.baseService.findById(id);
		if (null == t) {
			logger.warn("[id={}] is deleted!!", id);
			return mapping.findForward("err");
		}
		DynaBean dynaForm = (DynaBean) form;
		dynaForm.set("op", "update");
		dynaForm.set("vo", t);
		return mapping.findForward("success");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String op = request.getParameter("op");
		Integer userId = (Integer) request.getSession().getAttribute("user_id");
		T t = this.newValueObject();
		try {
			DynaBean dynaForm = (DynaBean) form;
			PropertyUtils.copyProperties(t, dynaForm.get("vo"));
		} catch (Exception e) {
			logger.warn("PropertyUtils", e);
			return mapping.findForward("err");
		}
		int ret = -1;
		if ("insert".equals(op)) {
			t.setCreate_by(userId);
			ret = this.baseService.insert(t);
		} else if ("update".equals(op)) {
			t.setUpdate_by(userId);
			ret = this.baseService.update(t);
		} else if ("delete".equals(op)) {
			t.setUpdate_by(userId);
			ret = this.baseService.delete(t);
		}
		if (ret > -1) {
			return mapping.findForward("success");
		}else {
			return mapping.findForward("err");
		}
	}

	public T newValueObject() {
		Class clazz = (Class) ((java.lang.reflect.ParameterizedType)
			    getClass().getGenericSuperclass()
			        ).getActualTypeArguments()[0];
		try {
			T t = (T) clazz.newInstance();
			return t;
		} catch (Exception e) {
			logger.warn("Generic Error:", e);
			return null;
		}
	}
}
